package com.itheima.reggie.service.impl;

import com.itheima.reggie.dao.UserDao;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.exception.CodeException;
import com.itheima.reggie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired(required = false)
    private UserDao userDao;


    @Override
    public User login(String phone, String code, String verifyCode) {
        User user=null;
//        判断验证码是否正确
        if (code.equals(verifyCode)) {
            //        判断数据库中是否有着手机号
           user=userDao.findByPhone(phone);
            if (user==null){
                //        2.没有创建
                user=new User();
                user.setPhone(phone);
                user.setStatus(1);
                userDao.save(user);
            }

        }else{
            throw  new CodeException("验证码错误");
        }
        return user;

    }
}
